package com.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.app.mapper.OrderMapper;
import com.app.mapper.UserMapper;
import com.app.model.*;
import com.app.mapper.CommentMapper;
import com.app.service.CommentService;
import com.app.service.UserService;
import com.app.utils.CommonHelp;
import com.app.utils.StringTool;
import com.app.vo.CommentVo;
import com.app.vo.IdCountVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 16693103420
 * @since 2024-01-22
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentModel> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderMapper orderMapper;

    //根据类型获取评论列表
    @Override
    public CommentVo getCommentByTypeList(Integer page, Integer limit, Integer relationId, Integer commentType) {
        Page<CommentModel> page1 = new Page<>(page, limit);
        QueryWrapper<CommentModel> qw = new QueryWrapper<>();
        qw.eq("relation_id", relationId);
        if (commentType != null && commentType != 1) {
            qw.eq("comment_type", commentType);
        }
        qw.orderByDesc("create_time");
        Page<CommentModel> commentPage = commentMapper.selectPage(page1, qw);
        List<CommentModel> commentList = commentPage.getRecords();
        for (CommentModel comment : commentList) {
            UserModel user = userMapper.getByOpenid(comment.getOpenId());
            comment.setUserName(user.getName());
            comment.setFaveUrl(user.getFaceUrl());
            //分数转文字
            Integer commentTypeInt = comment.getCommentType();
            if (commentTypeInt == 2) {
                comment.setScoreText("好评");//好评
            }
            if (commentTypeInt == 3) {
                comment.setScoreText("中评");//中评
            }
            if (commentTypeInt == 4) {
                comment.setScoreText("差评");//差评
            }
            //文件id转义
            if (comment.getFileId() != null) {
                List<String> url = JSON.parseObject(comment.getFileId(), new TypeReference<List<String>>() {
                });
                comment.setUrlList(url);
            }
            comment.setOpenId(null);
        }
        long count = commentPage.getTotal();
        CommentVo commentVo = new CommentVo();
        commentVo.setList(commentList);
        commentVo.setCount((int) count);

        List<IdCountVo> idCountVoList = new ArrayList<>();
        IdCountVo vo1 = new IdCountVo();
        vo1.setId(1);
        vo1.setName("最新");
        idCountVoList.add(vo1);
        for (int i = 2; i <= 4; i++) {
            IdCountVo vo = new IdCountVo();
            QueryWrapper<CommentModel> queryWrapperType = new QueryWrapper<>();
            queryWrapperType.eq("relation_id", relationId);
            queryWrapperType.eq("comment_type", i);
            Integer typeCount = commentMapper.selectCount(queryWrapperType);
            vo.setId(i);
            if (i == 2) {
                vo.setName("好评");
                vo.setCount(typeCount);
            }
            if (i == 3) {
                vo.setName("中评");
                vo.setCount(typeCount);
            }
            if (i == 4) {
                vo.setName("差评");
                vo.setCount(typeCount);
            }
            idCountVoList.add(vo);
        }
        commentVo.setCommentTypeList(idCountVoList);
        return commentVo;
    }

    //评论列表
    @Override
    public CommentVo getCommentList(Integer page, Integer limit, String startTime, String endTime, Integer commentType) {
        Page<CommentModel> page1 = new Page<>(page, limit);
        QueryWrapper<CommentModel> qw = new QueryWrapper<>();
        if (StringTool.isNotEmpty(startTime) && StringTool.isNotEmpty(endTime)) {
            qw.ge("create_time", startTime);
            qw.le("create_time", endTime);
        }
        if (commentType != null) {
            qw.eq("comment_type", commentType);
        }
        qw.orderByDesc("create_time");
        Page<CommentModel> commentPage = commentMapper.selectPage(page1, qw);

        List<CommentModel> commentList = commentPage.getRecords();
        for (CommentModel comment : commentList) {
            //分数转文字
            Integer commentTypeInt = comment.getCommentType();
            if (commentTypeInt == 2) {
                comment.setScoreText("好评");//好评
            }
            if (commentTypeInt == 3) {
                comment.setScoreText("中评");//中评
            }
            if (commentTypeInt == 4) {
                comment.setScoreText("差评");//差评
            }
            //文件id转义
            if (comment.getFileId() != null) {
                List<String> url = JSON.parseObject(comment.getFileId(), new TypeReference<List<String>>() {
                });
                comment.setUrlList(url);
            }
            comment.setOpenId(null);
        }
        long count = commentPage.getTotal();
        CommentVo commentVo = new CommentVo();
        commentVo.setList(commentList);
        commentVo.setCount((int) count);
        return commentVo;
    }

    //查询评论详情
    @Override
    public CommentModel getCommentById(Integer commentId) {
        CommentModel comment = commentMapper.selectById(commentId);
        UserModel user = userService.getUserByOpenid(comment.getOpenId());
        comment.setUserName(user.getName());
        comment.setFaveUrl(user.getFaceUrl());
        comment.setOpenId(null);
        Integer score = comment.getScore();
        if (score > 3) {
            comment.setScoreText("好评");//好评
        }
        if (score == 3) {
            comment.setScoreText("中评");//中评
        }
        if (score < 3) {
            comment.setScoreText("差评");//差评
        }
        //文件id转义
        if (comment.getFileId() != null) {
            List<String> url = JSON.parseObject(comment.getFileId(), new TypeReference<List<String>>() {
            });
            comment.setUrlList(url);
        }
        return comment;
    }

    //保存评论信息
    @Override
    public void userAddComment(CommentModel commentModel) {
        QueryWrapper<OrderModel> orderModelQueryWrapper = new QueryWrapper<>();
        orderModelQueryWrapper.eq("order_id", commentModel.getOrderId());
        OrderModel order = orderMapper.selectOne(orderModelQueryWrapper);
        //保存评论存息
        if (commentModel.getScore() > 3) {
            commentModel.setCommentType(2);//好评
        }
        if (commentModel.getScore() == 3) {
            commentModel.setCommentType(3);//中评
        }
        if (commentModel.getScore() < 3) {
            commentModel.setCommentType(4);//差评
        }
        commentModel.setFileId(JSON.toJSONString(commentModel.getUrl()));
        if (order != null) {
            commentModel.setRelationId(order.getBusinessId());
        }
        commentModel.setCreateTime(CommonHelp.getNowTimestamp());
        commentMapper.insert(commentModel);
        //订单状态修改
        if (order != null && order.getStatus() == 2) {
            order.setStatus(3);
            orderMapper.updateById(order);
        }
    }

    @Override
    public Double getScore(Integer id) {
        return commentMapper.getScore(id);
    }
}
